﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace Mandy_Utility
{
    public static class HttpHelper
    {
        private static string Request(string Url, string Method, string ContentType, string PostData, IDictionary<string, string> HttpHeader, int Timeout = 60 * 1000)
        {
            string Result = string.Empty;

            #region 参数不合法，直接返回空字符串

            if (string.IsNullOrWhiteSpace(Url))
            {
                return string.Empty;
            }

            if (string.IsNullOrWhiteSpace(Method))
            {
                return string.Empty;
            }

            if (string.IsNullOrWhiteSpace(ContentType))
            {
                return string.Empty;
            }

            if (Method.ToLower() != "get" && Method.ToLower() != "post")
            {
                return string.Empty;
            }

            #endregion

            HttpWebRequest Request = null;
            HttpWebResponse Response = null;

            Stream RequestStream = null;
            Stream ResponseStream = null;

            StreamReader SR = null;

            Request = WebRequest.Create(Url) as HttpWebRequest;

            CookieContainer cookieContainer = new CookieContainer();

            Request.CookieContainer = cookieContainer;
            Request.AllowAutoRedirect = true;
            Request.Method = Method;
            Request.Timeout = Timeout;
            Request.ContentType = ContentType;

            if (HttpHeader != null)
            {
                foreach (string Key in HttpHeader.Keys)
                {
                    Request.Headers.Add(Key, HttpHeader[Key]);
                }
            }

            if (Method.ToLower() == "post")
            {
                if (!string.IsNullOrEmpty(PostData))
                {
                    Encoding encoding = Encoding.GetEncoding("utf-8");
                    byte[] PostDataData = encoding.GetBytes(PostData);

                    Request.ContentLength = PostDataData.Length;

                    RequestStream = Request.GetRequestStream();
                    RequestStream.Write(PostDataData, 0, PostDataData.Length);
                    RequestStream.Close();

                }
            }

            try
            {
                Response = Request.GetResponse() as HttpWebResponse;

                ResponseStream = Response.GetResponseStream();

                SR = new StreamReader(ResponseStream);

                Result = SR.ReadToEnd();
            }
            catch (Exception ex)
            {
                Result = string.Empty;
            }

            return Result;
        }
        public static string Get(string Url, IDictionary<string, string> HttpHeader = null, int Timeout = 60 * 1000)
        {
            string Result = string.Empty;

            Result = Request(Url, "GET", "text/html; charset=utf-8", null, HttpHeader, Timeout);

            return Result;
        }
        public static T Get<T>(string Url, IDictionary<string, string> HttpHeader = null, int Timeout = 60 * 1000)
        {
            string Result = string.Empty;

            Result = Get(Url, HttpHeader, Timeout);

            return Result.ToJsonObject<T>();
        }
        public static string Post(string Url, string PostData, IDictionary<string, string> HttpHeader = null, int Timeout = 60 * 1000)
        {
            string Result = string.Empty;

            Result = Request(Url, "POST", "application/json", PostData, HttpHeader, Timeout);

            return Result;
        }
        public static string Post(string Url, object PostData, IDictionary<string, string> HttpHeader = null, int Timeout = 60 * 1000)
        {
            string Result = string.Empty;

            Result = Request(Url, "POST", "application/json", PostData.ToJsonString(), HttpHeader, Timeout);

            return Result;
        }
        public static T Post<T>(string Url, object PostData, IDictionary<string, string> HttpHeader = null, int Timeout = 60 * 1000)
        {
            string Result = string.Empty;

            Result = Post(Url, PostData.ToJsonString(), HttpHeader, Timeout);

            return Result.ToJsonObject<T>();
        }
    }
}
